package com.imooc.api.config;

import com.imooc.api.intercept.JWTCurrentUserInterceptor;
import com.imooc.api.intercept.SMSInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    /**
     * 在spring boot容器中放入拦截器
     */
    @Bean
    public SMSInterceptor smsInterceptor() {
        return new SMSInterceptor();
    }

//    @Bean
//    public RedisTokenInterceptor redisTokenInterceptor() {
//        return new RedisTokenInterceptor();
//    }

    @Bean
    public JWTCurrentUserInterceptor jwtInterceptor() {
        return new JWTCurrentUserInterceptor();
    }

    /**
     * 注册拦截器，并且拦截指定的路由，否则不生效
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

//        registry.addInterceptor(smsInterceptor()) // 短信频率限制
//                .addPathPatterns("/passport/getSMSCode");

//        registry.addInterceptor(redisTokenInterceptor())
//                .addPathPatterns("/c/**")
//                .addPathPatterns("/u/**");

        registry.addInterceptor(jwtInterceptor())
                .addPathPatterns("/**");

        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
